package cc.mrbird.febs.mark.service.impl;

import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import cc.mrbird.febs.job.entity.Job;
import cc.mrbird.febs.mark.entity.Railway;
import cc.mrbird.febs.mark.mapper.RailwayMapper;
import cc.mrbird.febs.mark.service.IRailwayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RailwayServiceImpl extends ServiceImpl<RailwayMapper, Railway> implements IRailwayService {

    @Value("10000")
    private int batchInsertMaxNum;


    @Override
    public List<Railway> findRailways() {
        List<Railway> railways=this.baseMapper.selectList(new QueryWrapper<>());
        return railways;
    }



    @Override
    public IPage<Railway> findRailways(QueryRequest request, Railway railway) {
        LambdaQueryWrapper<Railway> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(railway.getIsSeg())) {
            queryWrapper.eq(Railway::getIsSeg, railway.getIsSeg());
        }
        if (StringUtils.isNotBlank(railway.getIsEntity())) {
            queryWrapper.eq(Railway::getIsEntity, railway.getIsEntity());
        }
        if (StringUtils.isNotBlank(railway.getIsQuestion())) {
            queryWrapper.eq(Railway::getIsQuestion, railway.getIsQuestion());
        }
        if (StringUtils.isNotBlank(railway.getIsAssign())) {
            queryWrapper.eq(Railway::getIsAssign, railway.getIsAssign());
        }
        if (StringUtils.isNotBlank(railway.getFileName())) {
            queryWrapper.like(Railway::getFileName, railway.getFileName());
        }
        if (StringUtils.isNotBlank(railway.getStuNo())) {
            queryWrapper.eq(Railway::getStuNo, railway.getStuNo());
        }
        if (StringUtils.isNotBlank(railway.getTag())) {
            queryWrapper.eq(Railway::getTag, railway.getTag());
        }
        if (StringUtils.isNotBlank(railway.getCreateTimeFrom()) && StringUtils.isNotBlank(railway.getCreateTimeTo())) {
            queryWrapper
                    .ge(Railway::getCreateTime, railway.getCreateTimeFrom())
                    .le(Railway::getCreateTime, railway.getCreateTimeTo());
        }
        Page<Railway> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "createTime", FebsConstant.ORDER_DESC, true);
        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional
    public void updateRailway(Railway railway) {
        railway.setModifyTime(new Date());


        if(railway.getEntityWords()!=null){
            //设置存到数据库中的标注过的实体
            //更改标签<e1>text</e1>  为<e1 text /e1>
            String words,word2s;
            words=word2s=railway.getEntityWords();
            words=words.replaceAll("<e1>", "<e1 ");
            words=words.replaceAll("</e1>", " /e1>");
            words=words.replaceAll("<e2>", "<e2 ");
            words=words.replaceAll("</e2>", " /e2>");
            railway.setEntityWords(words);

            //设置前端显示的标注过的实体
            //更改标签<e1>text</e1>  为&lt;e1 text /e1&gt;

            word2s=word2s.replaceAll("<e1>", "&lt;e1 ");
            word2s=word2s.replaceAll("</e1>", " /e1&gt;");
            word2s=word2s.replaceAll("<e2>", "&lt;e2 ");
            word2s=word2s.replaceAll("</e2>", " /e2&gt;");
            railway.setEntityWordsFront(word2s);
        }
        updateById(railway);
    }

    @Override
    @Transactional
    public void batchInsert(List<Railway> list) {
        saveBatch(list, batchInsertMaxNum);
    }

    @Override
    @Transactional
    public void deleteRailways(String[] railwayIds) {
        List<String> list = Arrays.asList(railwayIds);
        this.baseMapper.deleteBatchIds(list);
    }

}

